package com.supermap.imanager.notification.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supermap.utils.HttpClientUtil;

public class ImocAlerm {
	public static final String IAM_TOKEN = "https://imoc.huawei.com/iam-auth/rest/v2/token/iamtoken";
	public static final String X_CSRF_TOKEN = "https://imoc.huawei.com/iam/v2/org/getxtoken";
	public static final String PUT_ALERM = "https://imoc.huawei.com/api-gateway/consume-service/services/data_access/metricAlarmService/putAlarm";

	private static String akvalue = "eyJ1dWlkIjoiMjJDOUQ3MDgtRTM2OS00OUM3LThGREEtOTVCMjU0MUQwMUI5IiwidXNlciI6ImNoYW90dSIsIl90ZW5hbnQiOiJIV0Nsb3VkSW5mcmEifQ==";
	private static String skvalue = "37572698228400834369035472876795";
	private static String tenant = "HWCloudInfra";
	private static String user = "chaotu";
	private static String treePath = "20_21_36";
	private static String monitorObject = "chaotu";
	private static String ecureProxyServer = "chaotu";

	private static JSONObject returnResult(int code, String value) {
		JSONObject resut = new JSONObject();
		resut.put("code", code);
		resut.put("value", value);

		return resut;
	}

	public static JSONObject getIamToken() {
		JSONObject resultJsonObject = null;
		JSONObject entity = new JSONObject();
		entity.put("akvalue", akvalue);
		entity.put("skvalue", skvalue);
		entity.put("tenant", tenant);
		entity.put("user", user);

		String iam_token = HttpClientUtil.RunPost(IAM_TOKEN, entity.toString(), null, "utf-8");
		if (null == iam_token) {
			return returnResult(1, "request is Exception, please check env.");
		}
		JSONObject jsonObject = JSONObject.parseObject(iam_token);

		if (Boolean.parseBoolean(jsonObject.getString("success").toString())) {
			resultJsonObject = returnResult(0, jsonObject.getString("data"));
			System.out.println("iam_token >>>>>>>>>>>>>>>>>>" + resultJsonObject.toString());
		}
		return resultJsonObject;
	}

	public static JSONObject getXCsrFToken() {
		JSONObject resultJsonObject = null;
		JSONObject header = new JSONObject();
		header.put("iam_user", user);

		String x_csr_token = HttpClientUtil.RunPost(X_CSRF_TOKEN, new JSONObject().toJSONString(), header, "utf-8");
		if (null == x_csr_token) {
			return returnResult(1, "request is Exception, please check env.");
		}
		JSONObject jsonObject = JSONObject.parseObject(x_csr_token);

		if (Boolean.parseBoolean(jsonObject.getString("success").toString())) {
			resultJsonObject = returnResult(0, jsonObject.getString("data"));
			System.out.println("iam_c_csrf_token >>>>>>>>>>>>>>>>>>" + resultJsonObject.toString());
		}
		return resultJsonObject;
	}

	public static JSONObject getAlerm(String metricName, String alarmdescription, String timestamp, String alarmlevel,
			boolean isAlerm) {
		JSONObject resultJsonObject = null;
		if ((int) getIamToken().get("code") == 0) {
			String iam_token = getIamToken().getString("value");
			if ((int) getXCsrFToken().get("code") == 0) {
				String x_csr_token = getXCsrFToken().getString("value");

				// 请求头，心跳和告警参数相同
				JSONObject header = new JSONObject();
				header.put("iam_user", user);
				header.put("iam_tenant", tenant);
				header.put("iam_akvalue", akvalue);
				header.put("skValue", skvalue);
				header.put("iam_token", iam_token);
				header.put("X-CSRF-TOKEN", x_csr_token);

				// 请求头，心跳和告警参数相同部分
				JSONObject entity = new JSONObject();
				entity.put("namespace", "Application");
				entity.put("user", user);
				entity.put("timestamp", timestamp);
				entity.put("treePath", treePath);
				entity.put("metricName", metricName);

				// 心跳，添加不同请求体参数
				entity.put("value", "2");

				JSONObject monitorO = new JSONObject();
				monitorO.put("key", "monitorObject");
				monitorO.put("value", monitorObject);

				JSONArray JsonArray = new JSONArray();
				JsonArray.add(monitorO);
				entity.put("tags", JsonArray);
				System.out.println("entity of hearth check >>>>>>>>>>>>>>>>>>" + entity.toString());
				JSONArray entity_hearth = new JSONArray();
				entity_hearth.add(entity);
				String heartbeat = HttpClientUtil.RunPost(PUT_ALERM, entity_hearth.toString(), header, "utf-8");
				if (null == heartbeat) {
					return returnResult(1, "request is Exception, please check env.");
				}
				JSONObject jsonObject_heartbeat = JSONObject.parseObject(heartbeat);

				if (jsonObject_heartbeat.getString("data").equalsIgnoreCase("ok")) {
					resultJsonObject = returnResult(0, jsonObject_heartbeat.getString("resultMessage"));
					if (!isAlerm) {
						// 心跳检测
						System.out.println("hearth check result>>>>>>>>>>>>>>  " + resultJsonObject.toString());
						return resultJsonObject;
					}

					// 报警,先移除心跳的参数，在添加告警请求体参数
					entity.remove("value");
					entity.remove("tags");
					entity.remove("tags");

					entity.put("value", "1");
					JSONObject monitorO_Arm = new JSONObject();
					monitorO_Arm.put("key", "monitorObject");
					monitorO_Arm.put("value", monitorObject);

					JSONObject ecurePS_Arm = new JSONObject();
					ecurePS_Arm.put("key", "ecureProxyServer");
					ecurePS_Arm.put("value", ecureProxyServer);

					JSONObject alarmL_Arm = new JSONObject();
					alarmL_Arm.put("key", "alarmLevel");
					alarmL_Arm.put("value", alarmlevel);

					JSONObject alarmD_Arm = new JSONObject();
					alarmD_Arm.put("key", "alarmDescription");
					alarmD_Arm.put("value", alarmdescription);

					JSONArray JsonArray_Arm = new JSONArray();
					JsonArray_Arm.add(monitorO_Arm);
					JsonArray_Arm.add(ecurePS_Arm);
					JsonArray_Arm.add(alarmL_Arm);
					JsonArray_Arm.add(alarmD_Arm);
					entity.put("tags", JsonArray_Arm);

					JSONArray entity_alerm = new JSONArray();
					entity_alerm.add(entity);
					System.out.println("entity of put alerm >>>>>>>>>>>>>>>>>>" + entity.toString());

					String alerm = HttpClientUtil.RunPost(PUT_ALERM, entity_alerm.toString(), header, "utf-8");
					if (null == alerm) {
						return returnResult(1, "request is Exception, please check env.");
					}
					JSONObject jsonObject_Arm = JSONObject.parseObject(alerm);

					if (jsonObject_Arm.getString("data").equalsIgnoreCase("ok")) {
						System.out.println("alerm put result >>>>>>>>>>>>>>  " + resultJsonObject.toString());
						resultJsonObject = returnResult(0, jsonObject_Arm.getString("resultMessage"));
					}
				}
			}
		}
		return resultJsonObject;
	}

	public static JSONObject restoreAlerm(String metricName, String alarmdescription, String timestamp,
			String alarmlevel) {
		JSONObject resultJsonObject = null;
		if ((int) getIamToken().get("code") == 0) {
			String iam_token = getIamToken().getString("value");
			if ((int) getXCsrFToken().get("code") == 0) {
				String x_csr_token = getXCsrFToken().getString("value");

				// 请求头，心跳和告警参数相同
				JSONObject header = new JSONObject();
				header.put("iam_user", user);
				header.put("iam_tenant", tenant);
				header.put("iam_akvalue", akvalue);
				header.put("skValue", skvalue);
				header.put("iam_token", iam_token);
				header.put("X-CSRF-TOKEN", x_csr_token);

				// 请求头，心跳和告警参数相同部分
				JSONObject entity = new JSONObject();
				entity.put("namespace", "Application");
				entity.put("user", user);
				entity.put("timestamp", timestamp);
				entity.put("treePath", treePath);
				entity.put("metricName", metricName);

				// 心跳，添加不同请求体参数
				entity.put("value", "2");

				JSONObject monitorO = new JSONObject();
				monitorO.put("key", "monitorObject");
				monitorO.put("value", monitorObject);

				JSONArray JsonArray = new JSONArray();
				JsonArray.add(monitorO);
				entity.put("tags", JsonArray);
				JSONArray entity_hearth = new JSONArray();
				entity_hearth.add(entity);
				String heartbeat = HttpClientUtil.RunPost(PUT_ALERM, entity_hearth.toString(), header, "utf-8");
				if (null == heartbeat) {
					return returnResult(1, "request is Exception, please check env.");
				}
				JSONObject jsonObject_heartbeat = JSONObject.parseObject(heartbeat);

				if (jsonObject_heartbeat.getString("data").equalsIgnoreCase("ok")) {
					resultJsonObject = returnResult(0, jsonObject_heartbeat.getString("resultMessage"));

					// 报警,先移除心跳的参数，在添加告警请求体参数
					entity.remove("value");
					entity.remove("tags");
					entity.remove("tags");

					entity.put("value", "0");
					JSONObject monitorO_Arm = new JSONObject();
					monitorO_Arm.put("key", "monitorObject");
					monitorO_Arm.put("value", monitorObject);

					JSONObject ecurePS_Arm = new JSONObject();
					ecurePS_Arm.put("key", "ecureProxyServer");
					ecurePS_Arm.put("value", ecureProxyServer);

					JSONObject alarmD_Arm = new JSONObject();
					alarmD_Arm.put("key", "alarmDescription");
					alarmD_Arm.put("value", alarmdescription);

					JSONArray JsonArray_Arm = new JSONArray();
					JsonArray_Arm.add(monitorO_Arm);
					JsonArray_Arm.add(ecurePS_Arm);
					JsonArray_Arm.add(alarmD_Arm);
					entity.put("tags", JsonArray_Arm);

					JSONArray entity_alerm = new JSONArray();
					entity_alerm.add(entity);
					System.out.println("entity of restore alerm >>>>>>>>>>>>>>>>>>" + entity.toString());

					String alerm = HttpClientUtil.RunPost(PUT_ALERM, entity_alerm.toString(), header, "utf-8");
					if (null == alerm) {
						return returnResult(1, "request is Exception, please check env.");
					}
					JSONObject jsonObject_Arm = JSONObject.parseObject(alerm);

					if (jsonObject_Arm.getString("data").equalsIgnoreCase("ok")) {
						System.out.println("restore alerm result >>>>>>>>>>>>>>  " + resultJsonObject.toString());
						resultJsonObject = returnResult(0, jsonObject_Arm.getString("resultMessage"));
					}
				}
			}
		}
		return resultJsonObject;
	}

}
